package com.dunqi.gpm.cangxi.home.activity;

import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.support.v4.widget.SwipeRefreshLayout;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.View;
import android.widget.TextView;

import com.chad.library.adapter.base.BaseQuickAdapter;
import com.dunqi.gpm.cangxi.R;
import com.dunqi.gpm.cangxi.common.api.ApiFactory;
import com.dunqi.gpm.cangxi.common.api.RxSchedulers;
import com.dunqi.gpm.cangxi.common.base.BaseAct;
import com.dunqi.gpm.cangxi.common.util.SwipeUtil;
import com.dunqi.gpm.cangxi.common.view.ComTitleBar;
import com.dunqi.gpm.cangxi.common.view.RecyclerItemDecoration;
import com.dunqi.gpm.cangxi.home.adapter.ProjectProblemAdapter;
import com.dunqi.gpm.cangxi.home.bean.Page;
import com.dunqi.gpm.cangxi.home.bean.ProjectProblem;
import com.dunqi.gpm.cangxi.home.constance.Constance;

import java.util.ArrayList;

import butterknife.BindView;
import butterknife.OnClick;
import io.reactivex.functions.Consumer;

public class QuestionHelpListActivity extends BaseAct implements BaseQuickAdapter.RequestLoadMoreListener, SwipeRefreshLayout.OnRefreshListener {

    @BindView(R.id.title_bar)
    ComTitleBar titleBar;
    @BindView(R.id.recycler)
    RecyclerView recycler;
    @BindView(R.id.swipeRefresh)
    SwipeRefreshLayout swipeRefresh;
    @BindView(R.id.solve_problem_tv)
    TextView solveProblemTv;
    private long projectProblemId;

    ArrayList<ProjectProblem> data = new ArrayList<>();
    private ProjectProblemAdapter adapter;
    private int problemStatus;

    @Override
    protected void refreshData(Bundle savedInstanceState) {
        net();
    }

    private void net() {
        ApiFactory.getInstance()
                .queryProblemCoordinationList(projectProblemId, page)
                .compose(RxSchedulers.<Page<ProjectProblem>>io_main())
                .subscribe(new Consumer<Page<ProjectProblem>>() {
                    @Override
                    public void accept(Page<ProjectProblem> pages) throws Exception {
                        ArrayList<ProjectProblem> list = pages.getList();

                        SwipeUtil.loadCompleted(swipeRefresh);

                        if (page == 1) {
                            adapter.replaceData(list);
                        } else {
                            adapter.addData(list);
                        }

                        if (pages.isHasNextPage()) {
                            adapter.loadMoreComplete();
                        } else {
                            adapter.loadMoreEnd(true);
                        }

                    }
                }, new Consumer<Throwable>() {
                    @Override
                    public void accept(Throwable throwable) throws Exception {
                        SwipeUtil.loadCompleted(swipeRefresh);
                        adapter.loadMoreFail();
                        t("获取数据失败");
                    }
                });
    }

    @Override
    protected void initUiAndListener() {
        projectProblemId = getIntent().getLongExtra(Constance.PROJECT_ID, 0);
        problemStatus = getIntent().getIntExtra(Constance.PROBLEMSTATUS, 0);

        if (problemStatus >= 6) {
            solveProblemTv.setVisibility(View.GONE);
        }

        recycler.setLayoutManager(new LinearLayoutManager(this));

        recycler.addItemDecoration(new RecyclerItemDecoration(1, RecyclerItemDecoration.VERTICAL));

        adapter = new ProjectProblemAdapter(data);
        recycler.setAdapter(adapter);

        adapter.setOnLoadMoreListener(this, recycler);

        SwipeUtil.init(swipeRefresh);
        swipeRefresh.setOnRefreshListener(this);
    }

    @Override
    public void onLoadMoreRequested() {
        net();
    }

    @Override
    public void onRefresh() {
        page = 1;
        net();
    }


    @Override
    protected int getLayoutId() {
        return R.layout.activity_question_help_list;
    }

    public static void start(Context context, long projectProblemId, int problemStatus) {
        Intent intent = new Intent(context, QuestionHelpListActivity.class);
        intent.putExtra(Constance.PROJECT_ID, projectProblemId);
        intent.putExtra(Constance.PROBLEMSTATUS, problemStatus);
        context.startActivity(intent);
    }

    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        if (resultCode == 607) {
            onRefresh();
        }
    }

    @OnClick(R.id.solve_problem_tv)
    public void onViewClicked() {
        SolveProblemActivity.start(this, projectProblemId, problemStatus);
    }
}
